import axios from 'axios'
const http = process.env.VUE_APP_BASE_API_COMMON

export function post(url,data) {
  return axios.post(http+url,data)
}

export function get(url,id) {
  return axios.get(http+url+id)
}

export function delete_(url,id) {
  return axios.delete(http+url+id)
}

export function put(url,id) {
  return axios.put(http+url+id)
}

export function initTableConfig(tableName, resolve) {
  axios.get(http+'table_config/'+tableName)
    .then(res=>{
      console.log(res.data)
      resolve(res.data)
    })
}

export function initFieldConfig(tableName, resolve) {
  const params = { 'params': { 'table_name': tableName }}
  axios.get(http + 'table_field/list', params)
    .then(res=>{
      console.log(res.data)
      resolve(res.data)
    })
}

export function initDict(tableName, resolve) {
  axios.get(http + 'dict/'+tableName)
    .then(res=>{
      console.log(res.data)
      resolve(res.data)
    })
}

export function getTableData(tableName, params, map, resolve) {
  const data = {}
  for (const key in params) {
    data[key] = params[key]
  }
  if (map && map.length != 0) {
    for (const key in map) {
      data[key] = map[key]
    }
    axios.post(http+tableName+'/list/condition',data)
      .then(res=>{
        console.log(res.data)
        resolve(res.data)
      })
  } else {
    axios.post(http+tableName+'/list',data)
      .then(res=>{
        console.log(res.data)
        resolve(res.data)
      })
  }
}

export function handlerConfig(data,resolve) {
  const new_data = []
  const tempMap = new Map()
  const mergeMap = new Map()
  for (let i = 0; i < data.length; i++) {
    if (data[i].merge) {
      if (tempMap.get(data[i].merge)) {
        const list = mergeMap.get(data[i].merge)
        list.push(data[i].fieldName)
        mergeMap.set(data[i].merge, list)
        const col = mergeInitConfig(tempMap.get(data[i].merge), data[i])
        tempMap.set(data[i].merge, col)
      } else {
        mergeMap.set(data[i].merge, [data[i].fieldName])
        tempMap.set(data[i].merge, data[i])
        new_data.push(data[i]) // 第一次的数据才需要进行添加到组中
      }
    } else {
      new_data.push(data[i])
    }
  }
  for (let i = 0; i < new_data.length; i++) {
    if (new_data[i].merge) {
      new_data[i] = tempMap.get(new_data[i].merge)
    }
  }
  resolve(mergeMap)
  return new_data
}

export function mergeInitConfig(col1, col2) {
  col1.fieldName = col1.merge
  col1.prop = col1.merge
  col1.label = col1.label + '' + col2.label
  col1.labelUs = col1.labelUs + '' + col2.labelUs
  return col1
}

export function handlerData(data,mergeMapping) {
  if (mergeMapping) {
    data.forEach(d => {
      mergeMapping.forEach((value, key) => {
        let vv = ''
        value.forEach(v => {
          vv += d[v]
        })
        d[key] = vv
      })
    })
  }
  return data
}

export function save(tableName, data, resolve) {
  axios.post(http + tableName+'/save', data)
    .then(res=>{
      resolve(res.data)
    })
}

export function update(tableName, data, resolve) {
  axios.put(http + tableName + '/update', data)
    .then(
      res => {
        resolve(res.data)
      })
}

export function del(tableName, id, resolve) {
  axios.delete(http + tableName + '/' + id)
    .then(
      res => {
        resolve(res.data)
      })
}

export function batchDel(tableName, ids, resolve) {
  axios.delete(http + tableName + '/batch/' + ids)
    .then(
      res => {
        resolve(res.data)
      })
}

export function commonDownloadFile(tableName, type, params, map, sels) {
  if (type == 1) { // 下载模板

  }
  const data = {}
  for (const key in params) {
    data[key] = params[key]
  }
}
